package org.idea.ietty.core.handle;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

import static org.idea.ietty.common.constants.IettyConstants.CONTENT_LENGTH;
import static org.idea.ietty.common.constants.IettyConstants.CONTENT_TYPE;
import static org.idea.ietty.common.constants.IettyConstants.ContentTypeEnum.*;

/**
 * 响应处理器
 *
 * @Author idea
 * @Date created in 10:02 下午 2020/5/6
 */
public class RespHandler {

    /**
     * 响应文本内容
     *
     * @param status
     * @param content
     * @return
     */
    public static FullHttpResponse reponseText(HttpResponseStatus status, ByteBuf content){
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
        if (content != null) {
            response.headers().set(CONTENT_TYPE, TEXT_TYPE.getContentTypeDes());
            response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        }
        return response;
    }

    /**
     * 响应json内容
     *
     * @param status
     * @param content
     * @return
     */
    public static FullHttpResponse responseJson(HttpResponseStatus status, ByteBuf content){
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
        if (content != null) {
            response.headers().set(CONTENT_TYPE, JSON_TYPE.getContentTypeDes());
            response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        }
        return response;
    }


    /**
     * 响应图片格式内容
     *
     * @param status
     * @param content
     * @return
     */
    public static FullHttpResponse responsePic(HttpResponseStatus status, ByteBuf content){
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
        if (content != null) {
            response.headers().set(CONTENT_TYPE, PIC_TYPE.getContentTypeDes());
            response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        }
        return response;
    }

}
